<?php
namespace Home\Controller;
use Think\Controller;
/*
 * 微商城(三级)结算控制器
 * @author <!--blakefan 1621743934@qq.com -->
 */
class SettlementController extends HomeController
{
	/*
	 *分配利润
	 *@param
	 */
	public function distriProfits()
	{
		
		$result=array();
		$ordersn=I('post.ordersn','','strip_tags');
		$uid=!empty($_SESSION['uid'])?$_SESSION['uid']:0;
		$sourceid=$uid;//下单人ID 
		if(empty($uid) || empty($ordersn))
		{
			$result['status']=3;
			$result['msg']='参数错误';
			echo josn_encode($result);
			return '';

		}

		
		//判断此订单是否属于此用户
		$order=M('order a');
		$temp=$order->where('a.order_sn=%s',$ordersn)->getField('uid');
		if($temp !=$uid)
		{	
			$result['status']=2;
			$result['msg']='系统繁忙，请稍后再试！';
			echo josn_encode($result);
			return '';
		}
		
		//计算订单中产品价格，默认使用（利润率百分比）
		$gross=$order->join(' inner join '.C('DB_PREFIX').'order_item  b  on a.orderid=b.orderid')->where("a.order_sn='%s'",$ordersn)->getField('recid, (b.price-b.supperprice)*b.number as grossprofit ');
		
		//计算订单的利润
		
		$totalgross=0;
		foreach($gross as $key=>$value)
		{
			//获取订单的纯利
			$totalgross+=$value;
			//默认使用30%
			$totalgross=$totalgross*0.3;
		}
		//为用户分配利润
		
		//获取平台用户ID
		$id=$this->getId($uid);
		$id=intval($id);
		//当平台用户存在时，及分配利润
		$user=M('user');
		$allocation=M('allocation_record');
		if(!empty($id))
		{
			//平台用户会得到订单纯利的50%
			
			$res=$user->where('uid=%d',$id)->setInc('money',$totalgross*0.3);
			
			if($res!==false)
			{

				$allocation->create();
				$allocation->order_sn=$ordersn;
				$allocation->money=$totalgross*0.3;
				$allocation->rate=0.3;
				$allocation->uid=$id;
				$allocation->sourceid=$sourceid;
				$allocation->gross=$totalgross;
				$allocation->addtime=time();
				$allocation->add();
			}
			
		}

		//为一级分销，二级分销，三级分销，分配利润
		for($i=1;$i<4;$i++)
		{

			
			$pid=$user->where('uid=%d',$uid)->getField('pid');
			if(empty($pid) && $i===1) break;
			switch ($i)
			{
					//一级分销
				case 1:
				$rate=0.5;
				break;
					//二级分销
				case 2:
				$rate=0.12;
				break;
				//三级分销
				case 3:
				$rate=0.08;
				break;
			}

			$res3=$user->where('uid=%d',$pid)->setInc('money',$totalgross*$rate);
			
			if($res3!==false)
			{
				$allocation->create();
				$allocation->order_sn=$ordersn;
				$allocation->money=$totalgross*$rate;
				$allocation->rate=$rate;
				$allocation->uid=$pid;
				$allocation->gross=$totalgross;
				$allocation->sourceid=$sourceid;
				$allocation->addtime=time();
				$temp3=$allocation->add();
			}

			$uid=$pid;

		}
		if($temp3==false){
			$result['status']=0;
			$result['msg']='系统繁忙，请稍后再试！';
			echo josn_encode($result);
			return '';
		}

		$result['status']=1;
		$result['msg']='结算成功';
		echo json_encode($result);

		
	}

	/*
	 *获取等级为：运营商Uid or 等级为商家的uid  
	 *@param uid
	 *return uid
	 */
	public  function getId($uid=0)
	{
		$uid=intval($uid);

		if(empty($uid)) return '';
		
		$user=M()->query('select pid,isshop,uid from  '.C('DB_PREFIX').'user where uid = '.$uid);

		if (empty($user))
		{
			return 0;
			
		}elseif(!empty($user) && !empty($user[0]['isshop']))
		{
			return $user[0]['uid'];

		}else{

			return $this->getId($user[0]['pid']);
		}
		
	}
}